package cn.ffcs.uec.wBStaffInfo.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.wBStaffInfo.action.bean.UecOutStaffEditBean;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class WbOutStaffEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecOutStaffEditBean bean = new UecOutStaffEditBean();

	//private String opType;
	@Resource
	private UecOrgOrgsManager uecOrgOrgsManager;

	private UecOutStaffInfo uecOutStaffInfo = new UecOutStaffInfo();
	private UecOutStaffManager uecOutStaffInfoManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);

		/**
		 * 1、在UecOutStaffEditComposer中通过addForward来监听UecOrgOrgsBandboxExt里触发的事件！
		 * 2、在onUecOrgOrgsEditSelectedResponse()方法里接收参数
		 */
		this.bean.getUecOrgOrgsBandboxExt().addForward(
				"onUecOrgOrgsEditSelected", this.self,
				"onUecOrgOrgsEditSelectedResponse");

	}


	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$wbuecOutStaffEditWin() throws Exception {
		this.bindCombobox();
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			uecOutStaffInfo.setPsnCode(screenName.toUpperCase());
		}
		if(uecOutStaffInfo!=null) {
			uecOutStaffInfo=uecOutStaffInfoManager.findUecOutStaffInfo(uecOutStaffInfo);
		}
			if (null != uecOutStaffInfo) {
				PubUtil.fillBeanFromPo(uecOutStaffInfo, bean);
				//性别
				if(!StrUtil.isNullOrEmpty(uecOutStaffInfo.getSex())) {
					if("1".equals(uecOutStaffInfo.getSex())) {
						bean.getSex().setSelectedIndex(0);
					}else {
						bean.getSex().setSelectedIndex(1);
					}
				}
				//
				UecOrgOrgs uecOrgOrgs = UecOrgOrgs.newInstance();
				if (null != uecOutStaffInfo.getPkHrorg()) {
					uecOrgOrgs.setPkOrg(uecOutStaffInfo.getPkHrorg());
					UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager
							.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
					if (null != findUecOrgOrgsByUecOrgOrgs) {
						bean.getUecOrgOrgsBandboxExt().setValue(
								findUecOrgOrgsByUecOrgOrgs.getName());
					}
				} else {
					bean.getUecOrgOrgsBandboxExt().setValue(null);
				}
				stringTransToDate();
			}
		}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
			PubUtil.fillPoFromEditorBean(uecOutStaffInfo, bean);
			DateFormatter();
			// 更新数据
			uecOutStaffInfo.updateOnly();
			Events.postEvent("onOK", this.self, uecOutStaffInfo);
			onCancel();
			Messagebox.show("数据修改成功！");
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecOutStaffEditWin().onClose();
	}

	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		// 绑定启用状态
		List<NodeVo> nodeVoEnableStateList = new ArrayList<NodeVo>();
		String[][] enableState = { { "1", "未启用" }, { "2", "已启用" },
				{ "3", "已停用" } };
		for (int i = 0; i < enableState.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(enableState[i][j]);
				nodeVo.setName(enableState[i][j + 1]);
			}
			nodeVoEnableStateList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getEnableState(),
				nodeVoEnableStateList);

		// 绑定性别sex:
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = { { "1", "男" }, { "2", "女" } };
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getSex(), nodeVoSexList);

		List<NodeVo> nodeVoIsList = new ArrayList<NodeVo>();
		String[][] is = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			nodeVoIsList.add(nodeVo);
		}
		// 是否装维
		ListboxUtils.rendererForEdit(bean.getIsZW(), nodeVoIsList);
		// 是否渠道
		ListboxUtils.rendererForEdit(bean.getIsQD(), nodeVoIsList);

		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = { { "1", "是" }, { "0", "否" } };
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}
		// 是否发送
		ListboxUtils.rendererForEdit(bean.getIsSend(), nodeVoIsSendList);
		// 是否发送
		ListboxUtils.rendererForEdit(bean.getDr(), nodeVoIsSendList);
		// 全日制学历
		ListboxUtils.rendererForEdit(bean.getFulltimeEducation(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 最高学历
		ListboxUtils.rendererForEdit(bean.getHighestEducation(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 学位
		ListboxUtils.rendererForEdit(bean.getDegree(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 绑定民族nationality
		ListboxUtils.rendererForEdit(bean.getNationality(),
				NodeVoUtil.getDictionaries(UecConstants.NATION));
		// 婚姻状况marital
		ListboxUtils.rendererForEdit(bean.getMarital(),
				NodeVoUtil.getDictionaries(UecConstants.MARRIAGE));
		// 健康状况
		ListboxUtils.rendererForEdit(bean.getHealth(),
				NodeVoUtil.getDictionaries(UecConstants.HELATH));
		// 政治面貌
		ListboxUtils.rendererForEdit(bean.getPolity(),
				NodeVoUtil.getDictionaries(UecConstants.POLITICAL_OUTLOOK));
		// 户口性质characterrpr
		ListboxUtils.rendererForEdit(bean.getCharacterrpr(),
				NodeVoUtil.getDictionaries(UecConstants.NATURE_REGISTRATION));
		// 进入电信系统来源--glbdef2
		ListboxUtils.rendererForEdit(bean.getGlbdef2(), NodeVoUtil
				.getDictionaries(UecConstants.ASSCESS_SOURCE_TELESTSTEM));

		// 是否是历史成员
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIshiskeypsn(),
				nodeVoIshiskeypsnList);
		// 是否是门店营业员
		ListboxUtils.rendererForEdit(bean.getIsshopassist(),
				nodeVoIshiskeypsnList);
		// 是否承包经营个人
		ListboxUtils.rendererForEdit(bean.getGlbdef14(), nodeVoIshiskeypsnList);
	}

	/**
	 * 校验
	 * 
	 * @return
	 */
	private String checkUacStaffData() {
		// BaseInfo Check
		if (StrUtil.isNullOrEmpty(bean.getPsnName().getValue())) {
			return "请填写人员名";
		} else if (StrUtil.checkBlank(bean.getPsnName().getValue())) {
			return "人员名中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getIdNum().getValue())) {
			return "请填写证件号码";
		} else if (StrUtil.checkBlank(bean.getIdNum().getValue())) {
			return "证件号码中有空格";
		}

		return null;
	}

	/**
	 * 将Date类型的日期转换成String类型
	 */
	public void DateFormatter() {
		// 出生日期
		/*uecOutStaffInfo.setBirthDate(DateUtil.dateToStr(bean.getBirthDate()
				.getValue()));
		// 参加工作日期
		uecOutStaffInfo.setJoinWorkDate(DateUtil.dateToStr(bean
				.getJoinWorkDate().getValue()));
		// 进入电信系统日期
		uecOutStaffInfo.setGlbdef3(DateUtil.dateToStr(bean.getGlbdef3()
				.getValue()));
		// 进入本单位日期
		uecOutStaffInfo.setEnterUnitTime(DateUtil.dateToStr(bean
				.getEnterUnitTime().getValue()));
		// 入党(团)日期
		uecOutStaffInfo.setJoinPolityDate(DateUtil.dateToStr(bean
				.getJoinPolityDate().getValue()));
		// 预计退休日期
		uecOutStaffInfo.setGlbdef15(DateUtil.dateToStr(bean.getGlbdef15()
				.getValue()));
		// 离退休日期
		uecOutStaffInfo.setRetireDate(DateUtil.dateToStr(bean.getRetireDate()
				.getValue()));
		// 身故日期
		uecOutStaffInfo.setDieDate(DateUtil.dateToStr(bean.getDieDate()
				.getValue()));*/
	}

	/**
	 * 将String类型的日期转换成Date类型
	 */
	public void stringTransToDate() throws ParseException {
		// 出生日期
		/*if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getBirthDate())) {
			bean.getBirthDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getBirthDate()));
		}*/
		// 参加工作日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getJoinWorkDate())) {
			bean.getJoinWorkDate()
					.setValue(
							DateUtil.patternStrToDate(uecOutStaffInfo
									.getJoinWorkDate()));
		}
		// 进入电信系统日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getGlbdef3())) {
			bean.getGlbdef3().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getGlbdef3()));
		}
		// 进入本单位日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getEnterUnitTime())) {
			bean.getEnterUnitTime().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo
							.getEnterUnitTime()));
		}
		// 入党(团)日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getJoinPolityDate())) {
			bean.getJoinPolityDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo
							.getJoinPolityDate()));
		}
		// 预计退休日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getGlbdef15())) {
			bean.getGlbdef15().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getGlbdef15()));
		}
		// 离退休日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getRetireDate())) {
			bean.getRetireDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getRetireDate()));
		}
		// 身故日期
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getDieDate())) {
			bean.getDieDate().setValue(
					DateUtil.patternStrToDate(uecOutStaffInfo.getDieDate()));
		}
	}

}
